{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "context=\"One is always on a strange road, watching strange scenery and listening to strange music. Then one day, you will find that the things you try hard to forget are already gone.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将这段需要统计的段落保存在context里面。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "context=context.replace(',','')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "context=context.replace('.','')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "逗号和句号换成空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "context=context.lower()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "统一换成小写字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "context=context.split()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用split方法进行分词，空格作为标识符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "dic=dict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建一个字典对象，用于保存每个单词，以及对应的单词出现次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "for key in context:\n",
    "    dic[key]=dic.get(key,0)+1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "循环找出context中的单词放入字典的键中，第一次出现的键对应的值为默认的0。get方法返回键对应的值，再次出现，值加1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "and 的出现次数： 1 \n",
      "\n",
      "you 的出现次数： 2 \n",
      "\n",
      "listening 的出现次数： 1 \n",
      "\n",
      "try 的出现次数： 1 \n",
      "\n",
      "find 的出现次数： 1 \n",
      "\n",
      "already 的出现次数： 1 \n",
      "\n",
      "day 的出现次数： 1 \n",
      "\n",
      "that 的出现次数： 1 \n",
      "\n",
      "things 的出现次数： 1 \n",
      "\n",
      "strange 的出现次数： 3 \n",
      "\n",
      "hard 的出现次数： 1 \n",
      "\n",
      "always 的出现次数： 1 \n",
      "\n",
      "watching 的出现次数： 1 \n",
      "\n",
      "is 的出现次数： 1 \n",
      "\n",
      "scenery 的出现次数： 1 \n",
      "\n",
      "the 的出现次数： 1 \n",
      "\n",
      "music 的出现次数： 1 \n",
      "\n",
      "on 的出现次数： 1 \n",
      "\n",
      "forget 的出现次数： 1 \n",
      "\n",
      "will 的出现次数： 1 \n",
      "\n",
      "to 的出现次数： 2 \n",
      "\n",
      "gone 的出现次数： 1 \n",
      "\n",
      "a 的出现次数： 1 \n",
      "\n",
      "road 的出现次数： 1 \n",
      "\n",
      "are 的出现次数： 1 \n",
      "\n",
      "then 的出现次数： 1 \n",
      "\n",
      "one 的出现次数： 2 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "for key in dic:\n",
    "    print(key,'的出现次数：',dic[key],'\\n')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
